use rbatis::rbdc::datetime::DateTime;
use rbatis::{crud, html_sql, htmlsql_select_page, impl_select, impl_select_page, py_sql, pysql_select_page, sql, Page, PageRequest, RBatis};
use serde::{Deserialize, Serialize};
use rbatis::rbatis_codegen::IntoSql;

#[allow(non_snake_case)]
#[warn(unused_imports)]
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Bank {
    resourceId: String,
    createTime: DateTime,
    createUser: String,
    editTime: DateTime,
    editUser: String,
    version: i8,
    number: String,
    parentNumber: Option<String>,
    name: String,
    address: String,
    phone: String,
}

crud!(Bank{});

impl_select!(Bank { select_by_id(id: &str) -> Option => "`where resourceId = #{id} limit 1`" });
impl_select_page!(Bank { select_page(id: &str) => "`where resourceId != #{id}`" });
pysql_select_page!(select_page_data(number: &str) -> Bank =>
    r#"`select * from bank where 1 = 1`
        if number != '':
            ` and number != #{number}`
        if !ids.is_empty():
            ` and id in `
            ${ids.sql()}"#);

htmlsql_select_page!(select_page_by_htmlsql(number: &str) -> Bank => "mapper/bank.html");

/// 自定义Bank实现
impl Bank {
    /// 测试pysql_select_page宏
    pub async fn select_page_data(rb: &RBatis, page_req: &PageRequest, number: &str) -> Page<Bank> {
        let result = select_page_data(rb, page_req, number).await;
        if let Ok(page) = result {
            page
        } else {
            Page::default()
        }
    }

    /// 测试htmlsql_select_page宏
    pub async fn select_page_by_htmlsql(rb: &RBatis, page_req: &PageRequest, number: &str) -> Page<Bank> {
        let result = select_page_by_htmlsql(rb, page_req, number).await;
        if let Ok(page) = result {
            page
        } else {
            Page::default()
        }
    }

    /// 测试sql、py_sql宏用法
    #[sql("select * from bank where number = ?")]
    pub async fn test_by_sql(rb: &RBatis, number: String) -> Option<Bank> {}

    #[py_sql("select * from bank where number != #{number} limit #{from}, #{size}")]
    pub async fn test_custom_page(rb: &RBatis, number: &str, from: u64, size: u64) -> Vec<Bank> {}

    /// 测试html_sql用法
    #[html_sql("mapper/bank.html")]
    pub async fn select_custom_html_sql(rb: &RBatis, number: &str) -> Vec<Bank> {}
}
